//<script type='text/javascript'>

win.push			= {};

win.push._proto_	= {
	update 	: function(vars) {
		xhttp.send('control.call.php', {trg:$(vars.id).ancestors()[1].id+".comment", wtype:'push', type:vars.page, tool:vars.tool, action:"shift", 'vars':vars});
	}
}



// TXTFIELD OBJECT
win.asset.txtfield	= {
    list	: {},
    current	: {},
    init	: function() {
        txtfloat = new Element("div", {id:"txtfloat", class:"txtfloat", style:"display:none"});
        
        init.langs.each( function(n){
            txtfloat.appendChild(new Element("font", {id:"txtfloat."+n, class:"lang"+((init.lang==n)?" select":"")})).update(n).observe('click',function(n){ win.asset.txtfield.swaplang(this); });
        });
        txtfloat.appendChild(new Element("font", {id:"txtfloat.descr",'class':"name"}));
        txtfloat.appendChild(new Element("input", {id:"txtfloat.field", type:"field"}));
        txtfloat.appendChild(new Element("div", {id:"txtfloat.div", class:"nicEdit-main"}));
        txtfloat.appendChild(new Element("div", {id:"txtfloat.ctrl", class:"ctrl"}));
        txtfloat.appendChild(new Element("div", {id:"txtfloat.comment", class:"comment"}));
        //if(vars.class) $(vars.id).addClassName(vars.class);
        
        $('body').appendChild(txtfloat);
        $('txtfloat').observe("mouseout",function(event){
            if(!event.relatedTarget.descendantOf(this) && event.relatedTarget != this && !nic.freeze) {
                win.asset.txtfield.blurfloat();
            }
        });
        
        /// EDITOR
        var niceCtrl = new nicEditor();
        niceCtrl.setPanel('txtfloat.ctrl');
        niceCtrl.addInstance('txtfloat.div');
        // not so sweet
        $("txtfloat.div").observe("click",function(){ if(!this.innerHTML) {this.blur(); this.focus()}; });
    },
    build	: function(vars) {
        $(vars.id).className="txtfield";
        if(vars.values[init.lang]) if(vars.rich) $(vars.id).update(vars.values[init.lang]); else $(vars.id).value = vars.values[init.lang];
        
        if(vars.rich) vars.values[init.lang] = win.asset.txtfield.seed(vars); else $(vars.id).type="field";
        
        $(vars.id).insert({before:new Element("font", {class:'fielddescr'}).update(vars.descr)});
        if(!vars.ref) vars.ref = vars.descr;
        vars.lang = (vars.values)? vars.values : {};
        vars.current = init.lang;
        win.asset.txtfield.list[vars.id] = vars;
        $(vars.id).observe('mouseover',function(){ win.asset.txtfield.setfloat(this.id); });
        
        if(!$('txtfloat')) win.asset.txtfield.init();
    },
    seed	: function(vars) {
        if(inlines = $$("div[id='"+vars.id+"'] div[title]:not(.inline)")) inlines.each(function(el){
            el.className = 'inline';
        });
        return (inlines)? $(vars.id).innerHTML : vars.values[init.lang];
    },
    unseed	: function(vars) {
        clone = $(vars.id).cloneNode(true);
        clone.id = vars.id+".clone";
        $("dashboard").appendChild(clone);
        $$("div[id='"+vars.id+".clone']  div.inline").each(function(el){ el.replace("<%"+el.title+"%>");});
        return clone.remove().innerHTML;
    },
    setfloat	: function(id) {
        vars = win.asset.txtfield.list[id];
        $("txtfloat").clonePosition(id, {setWidth:false, setHeight:false, offsetLeft:-2, offsetTop:-17});
        $("txtfloat.descr").update(vars.descr);
        
        trg = $((vars.rich)? "txtfloat.div":"txtfloat.field");
        trg[(vars.rich)?"innerHTML":"value"] = (vars.lang[vars.current])?vars.lang[vars.current]:("");
        $H({"txtfloat.div":vars.rich, "txtfloat.ctrl":vars.rich, "txtfloat.field":!vars.rich}).each(function(pair){ if(pair.value) $(pair.key).show(); else $(pair.key).hide(); });
        
        win.asset.txtfield.current = id;
        $("txtfloat").show();
        trg.focus();
        
    },
    blurfloat	: function() {
        id		= win.asset.txtfield.current;
        vars	= win.asset.txtfield.list[id];
        value	= (vars.rich) ? $("txtfloat.div").innerHTML : $("txtfloat.field").value;
        update = (value!=vars.lang[vars.current] && !(!value && typeof vars.lang[vars.current] == "undefined"));
        win.asset.txtfield.list[id].lang[vars.current] = value;
        $(id)[(vars.rich)?"innerHTML":"value"] = value;
        
        ///// UPDATE
        if(vars.direct && update) win.asset.txtfield.update(win.asset.txtfield.list[id]);
        $("txtfloat").hide();
    },
    swaplang	: function(trg) {
        lang	= trg.firstChild.nodeValue;
        vars	= win.asset.txtfield.list[win.asset.txtfield.current];
        field 	= $((vars.rich)? "txtfloat.div":"txtfloat.field");
        
        $("txtfloat."+lang).className = "lang select";
        $("txtfloat."+vars.current).className = "lang";
        vars.lang[vars.current] = field[(vars.rich)?"innerHTML":"value"];
        field[(vars.rich)?"innerHTML":"value"] = (vars.lang[lang])? vars.lang[lang] : "";
        vars.current = init.lang = lang;
        win.asset.txtfield.list[win.asset.txtfield.current] = vars;
        
        $H(win.asset.txtfield.list).each(function(pair) {
            win.asset.txtfield.list[pair.key].current = lang;
            $(pair.key)[(pair.value.rich)?"innerHTML":"value"] = (pair.value.lang[lang])? pair.value.lang[lang] : "";
        });
    },
    update 		: function(vars) {
        if(vars.rich && $$("div[id='"+vars.id+"']  div.inline").size()) vars.values[init.lang] = win.asset.txtfield.unseed(vars);
        xhttp.send('control.call.php', {trg:$(id).ancestors()[1].id+".comment", wtype:'push', 'tool':"text.element", action:'edit', 'vars':vars});
        if(vars.rich) vars.values[init.lang] = $(vars.id).innerHTML; 
        if(vars.traceback && vars.current==init.lang) {
            vars.traceback.each(function(id) { $(id).update(vars.lang[vars.current]) });
        }
    }
}

// AJAX INPUT OBJECT
win.asset.ajxfield	= {
    list	: {},
    build	: function(vars) {
        $(vars.id).className="ajxfield";
        $(vars.id).value = vars.meta.value;
        
        $(vars.id).insert({before:new Element("font", {class:'fielddescr'}).update(vars.descr)});
        win.asset.ajxfield.list[vars.id] = vars;
        $(vars.id).observe('blur',function(){ win.asset.ajxfield.update(this.id); });
    },
    
    update 		: function(id) {
        if(win.asset.ajxfield.list[id].meta.value != $(id).value) {
            win.asset.ajxfield.list[id].meta.value = $(id).value;
            vars = win.asset.ajxfield.list[id];
            prnt = $(id).ancestors();
            
            xhttp.send('control.call.php', {trg:((prnt[1].id)? prnt[1].id:prnt[4].id)+".comment", wtype:'push', 'tool':vars.tool, action:'ajax', 'vars':vars});
        }
    }
}


// SHFT OBJECT
win.asset.shift		= {
	list	: {},
	build	: function(vars) {
		$(vars.id).className = "shift";
		$(vars.id).appendChild(new Element("div", {id:vars.id+".choice",class:"choice"}));
		$(vars.id+".choice").appendChild(new Element("div", {id:vars.id+".handle",class:"handle"}));
		$(vars.id).appendChild(new Element("div", {id:vars.id+".sub",class:"sub"}).update((vars.meta.value)?"yes":"no"));
		$(vars.id).appendChild(new Element("font", {id:vars.id+".descr",class:"descr"}).update(vars.descr));
		
		win.asset.shift.list[vars.id] = vars;
		if(vars.meta.value) $(vars.id).addClassName("active");
		$(vars.id+".choice").observe("click", function() {
			value = win.asset.shift.list[this.parentNode.id].meta.value = ( !win.asset.shift.list[this.parentNode.id].meta.value )? 1 : 0;
			
			$(this.parentNode.id+".sub").update((value)? "yes" : "no");
			this.parentNode.className = (value)? "shift active" : "shift";
			win.asset.shift.update(win.asset.shift.list[this.parentNode.id]);
		});
	},
	update 	: function(vars) {
		xhttp.send('control.call.php', {trg:$(vars.id).ancestors()[1].id+".comment", wtype:'push', type:vars.page, tool:vars.tool, action:"shift", 'vars':vars});
	}
}





/////// TEXTFIELD OBJECT (auto-update)
var TextField = function( vars ) {
	
	Object.extend( obj = new Element( "input", { class: "textfield txtfield", type: "field", value: vars.values[ init.lang ]}), win.asset.textfield );
	obj.vars = vars;
	obj.insert({ before: new Element( "span", { class:"fielddescr" }).update( vars.descr )});
	
	obj.observe( "mouseover", controls.bubble ).objtype = "text";

	return obj;
}

win.asset.textfield	= {
	
	
	reload		: function( oldlang, newlang ) {
		console.log( this );
		this.vars.lang[ oldlang ] = this.value;
		this.value = this.vars.lang[ newlang ];
	},
	
	
	update 		: function( e ) {
		if( this.vars.meta.value != this.value ) {
			this.vars.meta.value = this.value;
			xhttp.send('control.call.php', { trg:this.id, wtype:'push', tool:this.vars.tool, action:'ajax', vars:this.vars});
		}
    }
}

/////// AJX INPUT OBJECT (auto-update)
var Ajxfield = function (vars) {
	obj = new Element("div", { class:"ajxfield_container" }).update( vars.descr );
	Object.extend(inp = new Element("input", {id:vars.id, class:"ajxfield", value: vars.meta.value }), win.asset.ajxfieldMK2);
	Object.extend(inp.vars = vars, vars.meta);
	obj.insert( inp.observe('blur', inp.update ) );

	return obj;
}

win.asset.ajxfieldMK2	= {
	update 		: function( e ) {
		if( this.vars.meta.value != this.value ) {
			this.vars.meta.value = this.value;
			xhttp.send('control.call.php', { trg:this.id, wtype:'push', tool:this.vars.tool, action:'ajax', vars:this.vars});
		}
    }
}
//Object.extend(win.asset.ajxfieldMK2, win.push._proto_);

/////// OPT (choose) OBJECT
var Opt = function (vars) {
	Object.extend(obj = new Element("div", {id:vars.id, class:"opt"}), win.asset.opt);
	Object.extend(obj.vars = vars, vars.meta);
	
	obj.appendChild(new Element("font", {id:vars.id+".value",class:"value"}).update(vars.value));
	obj.appendChild(new Element("div", {id:obj.id+".descr",class:"descr"}).update(vars.descr));
	obj.observe('click', function(){ this.pop(); });
	return obj;
}

win.asset.opt	= {
	pop 	: function() {
		$("dark").toggle();
		$("pop").update( new List({id:this.id+".lst", tool:this.vars.tool, descr:this.vars.descr, 'new':'add '+this.vars.type, meta:this.vars}) );
		
		xhttp.send('control.call.php', {trg:this.id+".lst", wtype:'push', type:this.vars.type, tool:this.vars.tool, action:"pull", 'vars':this.vars});
	},
	request	: function() {
		//
	}
}
Object.extend(win.asset.opt, win.push._proto_);

/////// SHIFT (I/O) OBJECT
var Shift = function (vars) {
	Object.extend(obj = new Element("div", {id:vars.id, class:"shift"}), win.asset.shiftMK2);
	Object.extend(obj.vars = vars, vars.meta);
	
	obj.appendChild( choice = new Element("div", {id:obj.id+".choice",class:"choice"}));
	obj.appendChild(new Element("font", {id:obj.id+".descr",class:"descr"}).update(vars.descr));
	obj.observe("click", function(){ this.send(); });
	if(obj.vars.value) choice.addClassName("active");
	return obj;
}

win.asset.shiftMK2	= {
	send 	: function() {
		$(this.id + ".choice").toggleClassName("active");
		this.vars.value = this.vars.meta.value = !this.vars.value;
		xhttp.send('control.call.php', {trg:this.ancestors()[2].id+".comment", wtype:'push', type:this.vars.type, tool:this.vars.tool, action:"shift", 'vars':this.vars});
	}
}
Object.extend(win.asset.shiftMK2, win.push._proto_);

/////// BUTTON OBJECT
var Button = function (vars) {
	Object.extend(obj = new Element("input", {id:vars.id, class:"wide", type:"button", value:vars.descr}), win.asset.button);
	Object.extend(obj.vars = vars, vars.meta);
	
	if( !vars.func ) vars.func = "send";
	obj.observe('click', obj[vars.func] );

	return obj;
}

win.asset.button	= {
	send 	: function() {
		this.vars.value = this.vars.meta.value = !this.vars.value;
		xhttp.send('control.call.php', {trg:this.id, wtype:'push', type:this.vars.type, tool:this.vars.tool, action:this.vars.action, 'vars':this.vars});
	},
	
	window	: function() {
		this.vars.vars = this.vars.meta; 
		$('column_right').insert( new Win( this.vars.id + ".win." + Math.random() ).request( this.vars ) );
	}
}
Object.extend(win.asset.button, win.push._proto_);




























